package com.base.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.base.mapper.SpringScheduledCronMapper;
import com.base.utils.SpringUtils;
import com.base.vo.SpringScheduledCron;

/**
 * @author wnhuang
 * @Package com.base.api
 * @date 2021-02-24 16:38
 */
public interface ScheduledOfTask extends Runnable{

    /**
     * 定时任务方法
     */
    void execute();
    /**
     * 实现控制定时任务启用或禁用的功能
     */
    @Override
    default void run() {
        SpringScheduledCronMapper repository = SpringUtils.getBean(SpringScheduledCronMapper.class);
        QueryWrapper<SpringScheduledCron> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cron_key",this.getClass().getName());
        SpringScheduledCron scheduledCron = repository.selectOne(queryWrapper);
        if (scheduledCron.getStatus()==1) {
            // 任务是禁用状态
            return;
        }
        execute();
    }
}
